package com.example.qinghubeta;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qinghubeta.Sql.Mysqlist;
import com.example.qinghubeta.Sql.UserDao;

public class Register extends AppCompatActivity implements View.OnClickListener {

    private SQLiteDatabase sdb;
    private Mysqlist mys;
    private TextView reguser, regpwd, reregpwd;
    private Button regbtn;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initview();
        sqlite();
        setClick();

    }

    private void setClick() {
        regbtn.setOnClickListener(this);
    }

    private void sqlite() {
        mys = new Mysqlist(this, "zhu_c", null, 1);//使用halper创建数据库
        sdb = mys.getWritableDatabase();
    }

    private void initview() {
        regbtn = findViewById(R.id.regbtn);
        reguser = findViewById(R.id.reguser);
        reregpwd = findViewById(R.id.reregpwd);
        regpwd = findViewById(R.id.reregpwd);
    }

    @Override
    public void onClick(View view) {
        sharedPreferences = getSharedPreferences("auth",MODE_PRIVATE);
        int r = sharedPreferences.getInt("auth",1);
        switch (view.getId()) {
            case R.id.regbtn:
                if (r==2){
                    String name = reguser.getText().toString().trim();
                    String pass = regpwd.getText().toString().trim();
                    if (!regpwd.getText().toString().trim().equals(reregpwd.getText().toString().trim())){
                        Toast.makeText(getApplicationContext(),"两次输入不一致",Toast.LENGTH_SHORT).show();
                    }else {

                        if (name == null || "".equals(name) || pass == null || "".equals(pass)) {
                            Toast.makeText(this, "账号与密码不能为空", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                            sdb.execSQL("insert into user_mo(name,pass)values('" + name + "','" + pass + "')");
                            Intent i = new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(i);
                            break;
                        }
                    }
                }
                else if (r==4){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String name = reguser.getText().toString().trim();
                            String pass = regpwd.getText().toString().trim();
                            UserDao ud = new UserDao();
                            boolean result =ud.register(name,pass);
                            if (!result){
                                Looper.prepare();
                                Toast toast = Toast.makeText(getApplicationContext(),"注册成功！",Toast.LENGTH_SHORT);
                                toast.show();
                                Intent i = new Intent(getApplicationContext(),MainActivity.class);
                                startActivity(i);
                                Looper.loop();

                            }

                            //以上为jdbc注册
                        }
                    }).start();

                }
                break;

        }
    }
}
